$(function(){
    if (!String.prototype.trim) {
        String.prototype.trim = function () {
            return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
        };
    }
    $('#changePass').validate({
        rules: {
            oldPass: {
                required: true,
                rangelength: [8,20]
            },
            newPass: {
                required: true,
                rangelength: [8,20],
                checkPassword:true
            },
            sureNewPass: {
                required: true,
                rangelength: [8,20],
                checkPassword:true,
                equalTo: "#newPass"
            }
        },
        messages: {
            oldPass: {
                required: '原登录密码不能为空',
                rangelength: '密码长度为8-20位'
            },
            newPass: {
                required: '新密码不能为空',
                rangelength: '密码长度为8-20位'
            },
            sureNewPass: {
                required: '确认密码不能为空',
                rangelength: '密码长度为8-20位',
                equalTo:"两次输入的密码不一致"
            }
        },
        debug: true,
        focusInvalid: false,
        onkeyup: false,
        success : "valid",
        submitHandler: function(){
            $('#changePass').find(":submit").attr("disabled", true);
            var oldPassword = $('#oldPass').val().trim();
            var newPassword1 = $('#newPass').val().trim();
            var newPassword2 = $('#sureNewPass').val().trim();
            $.ajax({
                url: '/changePasSword',
                data:{
                    oldPassword: oldPassword,
                    newPassword1: newPassword1,
                    newPassword2: newPassword2
                },
                type: 'post',
                dataType: 'json',
                success: function(data){
                    if(data.success){
                        window.location.href= '/login'
                    }else{
                        $.errorHandler('#changePass','',data.msg);
                        //console.log(data)
                    }
                    $('#changePass').find(":submit").attr("disabled", false);
                },
                error: function(){}
            });
        }
    });
    $('#sureNewPass').focus(function () {
       /* $(this).val('');*/
        $('div.error').remove();
    })
});